package com.crl.rcdp.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/13 0:22
 */
public class SlowLogFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("记录慢查询");//TODO
        //调用请求之前统计时间
        Long startTime = System.currentTimeMillis();
        return chain.filter(exchange).then().then(Mono.fromRunnable(() -> {
            //调用请求之后统计时间
            Long endTime = System.currentTimeMillis();
            long consume = endTime - startTime;
            if (consume > 3000) { //请求超过三秒
                //写入数据库 TODO
            }
        }));
    }

    @Override
    public int getOrder() {
        return 200;
    }
}
